﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TitipNyobaYan.Views
{
    public partial class ViewDenda : Form
    {
        Controllers.Control<Models.DENDA> conDenda;
        Controllers.Control<Models.ORANG> conOrang;

        public ViewDenda()
        {
            InitializeComponent();
            conDenda = new Controllers.Control<Models.DENDA>();
            conOrang = new Controllers.Control<Models.ORANG>();
            dgDenda.DataSource = conDenda.view();
            loadCmbOrang();
        }

        private void loadCmbOrang()
        {
            cmbIDOrang.DataSource = conOrang.view().Select((c) => new { c.ID_ORANG }).ToList();
        }

        private void EditDenda(object sender, EventArgs e)
        {
            var denda = conDenda.get(txtIDDenda.Text);
            denda.ID_DENDA = txtIDDenda.Text;
            denda.ID_ORANG = conOrang.get(cmbIDOrang.SelectedText);
            denda.JUMLAH_DENDA = txtJumlahDenda.Text;
            denda.STATUS_DENDA = bool.Parse(txtStatusDenda.Text);
            conDenda.edit(denda);
            dgDenda.DataSource = conDenda.view();
        }

        private void tambahDenda_Click(object sender, EventArgs e)
        {
            var denda = new Models.DENDA
            {
                ID_DENDA = txtIDDenda.Text,
                //ID_ORANG = conOrang.get(cmbIDOrang.SelectedText),
                JUMLAH_DENDA = txtJumlahDenda.Text,
                STATUS_DENDA = bool.Parse(txtStatusDenda.Text)
            };
            conDenda.tambah(denda);
            dgDenda.DataSource = conDenda.view();
        }

        private void hapusDenda(object sender, EventArgs e)
        {
            conDenda.hapus(txtIDDenda.Text);
            dgDenda.DataSource = conDenda.view();
        }

        private void dgDenda_SelectionChanged(object sender, EventArgs e)
        {
            if (dgDenda.SelectedRows.Count != 1) return;
            try
            {
                var denda = conDenda.get(dgDenda.SelectedRows[0].Cells[0].Value.ToString());
                txtIDDenda.Text = denda.ID_DENDA;
                cmbIDOrang.SelectedText = (denda.ID_ORANG == null) ? "-" : denda.ID_ORANG.ID_ORANG;
                txtJumlahDenda.Text = denda.JUMLAH_DENDA;
                txtStatusDenda.Text = denda.STATUS_DENDA.ToString();
            }
            catch { }
        }
    }
}
